package com.vogue.supplymall.order.domain.biz.impl;

import com.vogue.supplymall.common.domain.dto.SearchOrderInfo;
import com.vogue.supplymall.common.domain.dto.SearchPrepayInfo;
import com.vogue.supplymall.common.domain.dto.UserInfo;
import com.vogue.supplymall.order.domain.biz.OrderListBiz;
import com.vogue.supplymall.order.domain.biz.PrepaySearchBiz;
import com.vogue.supplymall.order.domain.service.OrderService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * The type Order detail biz.
 *
 * @aouthor: zhangliang
 * @date: Create in 2018-02-06
 */
@Service("orderListBiz")
public class OrderListBizImpl implements OrderListBiz {
    
    public static final Logger LOGGER_ORDERLISTBIZIMPL= LoggerFactory.getLogger(OrderListBizImpl.class);

    @Resource
    private OrderService orderService;
    @Resource
    private PrepaySearchBiz prepaySearchBiz;

    @Override
    public List<Map<String, Object>> findOrderList(SearchOrderInfo searchOrderInfo, UserInfo userInfo) {


        List<Map<String, Object>> result = orderService.findOrderList(searchOrderInfo);
        LOGGER_ORDERLISTBIZIMPL.info("findOrderList >> result.size : " + result.size());


        List<Map<String, Object>> dataList = new ArrayList<>();
        // 将订单分组 取出订单的集合
//        Map<Object, Object> group = new HashMap<>();
        List<java.math.BigInteger> foreachList = new ArrayList<>();
        result.parallelStream().forEachOrdered((map) -> {
            if (!foreachList.contains(map.get("orderid"))) {
                foreachList.add((java.math.BigInteger) map.get("orderid"));
            }
//            group.put(map.get("orderid"), "");
            if ("1".equals(map.get("iscancel").toString())) {
                map.put("iscancel", true);
            } else {
                map.put("iscancel", false);
            }

        });

        foreachList.parallelStream().forEachOrdered((orderid) -> {
            List<Map<String, Object>> goodsData = new ArrayList<>();
            Map<String, Object> order = new HashMap<>();
            result.stream().forEach((good) -> {
                if (orderid.equals(good.get("orderid"))) {
                    goodsData.add(good);
                    order.put("orderid", good.get("orderid"));
                    order.put("statusId", good.get("statusId"));
                    order.put("orderprice", good.get("orderprice"));
                    order.put("totalquantity", good.get("totalquantity"));
                    order.put("groupId", good.get("groupId"));
                    order.put("shopId", good.get("shopId"));
                    order.put("groupName", good.get("groupName"));
                    order.put("shopName", good.get("shopName"));
                    order.put("selltel", good.get("selltel"));
                    order.put("billState", good.get("billState"));
                    order.put("iscancel", good.get("iscancel"));
                    order.put("paymentmode", good.get("paymentmode"));
                    order.put("sellpullstate", good.get("sellpullstate"));
                    // 2018/7/2 添加买家拉取状态
                    order.put("buypullstate", good.get("buypullstate"));
                    order.put("freight", good.get("freight"));
                    order.put("ordernumber", good.get("ordernumber"));
                    order.put("ordercreatedate", good.get("ordercreatedate"));
                    order.put("ordermodifydate", good.get("ordermodifydate"));
                    order.put("buytel", good.get("buytel"));
                    order.put("buyorganname", good.get("buyorganname"));
                    order.put("buyertype", good.get("buyertype"));
                    order.put("cancelstate", good.get("cancelstate"));
                    // 2018/7/12 添加卖家mcid
                    order.put("mcid", good.get("mcid"));

                    // 2018/10/29 查询预付款余额
                    SearchPrepayInfo searchPrepayInfo = new SearchPrepayInfo();
                    searchPrepayInfo.setSyscodeid(userInfo.getSyscodeid());
                    searchPrepayInfo.setOrganid(userInfo.getOrganInfo().getId());
                    searchPrepayInfo.setSellorganid(new Long(good.get("sellorganid").toString()));
                    searchPrepayInfo.setSellsyscodeid(new Long(good.get("sellsyscodeid").toString()));
                    Map<String, String> prepay = prepaySearchBiz.searchPrepayBalance(searchPrepayInfo);
                    order.put("prepaidValue", prepay.get("balance"));
                    order.put("paymode", prepay.get("paymode"));
                }
            });

            order.put("goodsData", goodsData);
            dataList.add(order);
        });

//        group.forEach((key, value) -> {
//            List<Map<String, Object>> goodsData = new ArrayList<>();
//            Map<String, Object> order = new HashMap<>();
//            result.stream().forEach((good) -> {
//                if (key.equals(good.get("orderid"))) {
//
//                    goodsData.add(good);
//                    order.put("orderid", good.get("orderid"));
//                    order.put("statusId", good.get("statusId"));
//                    order.put("orderprice", good.get("orderprice"));
//                    order.put("totalquantity", good.get("totalquantity"));
//                    order.put("groupId", good.get("groupId"));
//                    order.put("shopId", good.get("shopId"));
//                    order.put("groupName", good.get("groupName"));
//                    order.put("shopName", good.get("shopName"));
//                    order.put("billState", good.get("billState"));
//                    order.put("iscancel", good.get("iscancel"));
//                    order.put("paymentmode", good.get("paymentmode"));
//                    order.put("sellpullstate", good.get("sellpullstate"));
//                }
//            });
//
//            order.put("goodsData", goodsData);
//            dataList.add(order);
//        });

        return dataList;
    }

    @Override
    public List<Map<String, Object>> findOrderCount(SearchOrderInfo searchOrderInfo) {

        List<Map<String, Object>> result = orderService.findOrderCount(searchOrderInfo);
        LOGGER_ORDERLISTBIZIMPL.info("findOrderList >> result.size : " + result.size());


        List<Map<String, Object>> dataList = new ArrayList<>();
        Map<String, Object> data = result.get(0);

        Map<String, Object> order = new HashMap<>();

        order.put("billState", 0);
        order.put("count", data == null ? 0 : data.get("state0"));
        dataList.add(order);

        order = new HashMap<>();
        order.put("billState", 1);
        order.put("count", data == null ? 0 : data.get("state1"));
        dataList.add(order);

        order = new HashMap<>();
        order.put("billState", 2);
        order.put("count", data == null ? 0 : data.get("state2"));
        dataList.add(order);

        order = new HashMap<>();
        order.put("billState", 3);
        order.put("count", data == null ? 0 : data.get("state3"));
        dataList.add(order);

        order = new HashMap<>();
        order.put("billState", 4);
        order.put("count", data == null ? 0 : data.get("state4"));
        dataList.add(order);

        order = new HashMap<>();
        order.put("billState", 5);
        order.put("count", data == null ? 0 : data.get("state5"));
        dataList.add(order);

        return dataList;
    }


}
